package com.dcy.znoa.common.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.util.DateUtil;
import com.dcy.znoa.dao.attendance.AttRealRecordDao;
import com.dcy.znoa.dao.attendance.AttRecordDao;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.attendance.AttRealRecord;
import com.dcy.znoa.domain.attendance.AttRecord;
import com.dcy.znoa.domain.publiz.Company;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@EnableScheduling
@Component
public class NotHitTask implements ScheduledTask {

    @Resource
    AttRecordDao attRecordDao;
    @Resource
    AttRealRecordDao attRealRecordDao;
    @Resource
    OperDao operDao;

    @Scheduled(cron="0 0 1 * * ?")
//    @Scheduled(fixedRate=5000)
    @Transactional
    @Override
    public void execute() {
        //获取昨天时间
        String yesterdayStr = DateUtil.getYesterdayYMD();
        Date yesterday = DateUtil.getYesterday();
        log.info("NotHitTask execute start ,date = " + yesterdayStr);
        // 查询所有人，轮询，看这个人的考勤规则。如果没有规则就不管，
        // 有规则就按这个人的创建时间到今天早上0点为止，来查询有误上下班记录。没有的话就设置为未打卡
        List<Oper> operList = operDao.selectList(new EntityWrapper<>());
        for (Oper oper : operList) {
            Long compId = oper.getCompId();
            if (oper.getAttRuleNo() == null || compId == null) {
                continue;//没有归属公司或考勤规则，就不用打卡
            }
            Long operId = oper.getOperId();
            //查询这个人昨天有没有上下班打卡
            List<AttRealRecord> attRealRecordList = attRealRecordDao.selectList(new EntityWrapper<AttRealRecord>().eq(("oper_id"), operId).eq("att_time", yesterdayStr));
            if (attRealRecordList.size() == 2) {//上班和下班的只有一条，是由 AttRealRecordController 来控制的
                continue;
            }
            boolean typeUp = false;
            boolean typeDown = false;
            for (AttRealRecord attRealRecord : attRealRecordList) {
                Integer recordType = attRealRecord.getRecordType();
                if (recordType != null) {
                    if (recordType == 1) {//上班
                        typeUp = true;
                    } else if (recordType == 2) {
                        typeDown = true;
                    } else {
                        //do nothing
                    }
                }
            }
            if (typeUp == false) {//加一个未打上班卡给数据库
                AttRecord attRecord = new AttRecord(operId, compId, 1,9,9, yesterday);
                attRecordDao.insert(attRecord);
                AttRealRecord upNotHitRecord = new AttRealRecord(attRecord.getRecordId() , operId, compId, 1, yesterday);
                attRealRecordDao.insert(upNotHitRecord);
            }
            if (typeDown == false) {//加一个未打下班卡给数据库
                AttRecord attRecord = new AttRecord(operId, compId, 1, 9,9,yesterday);
                attRecordDao.insert(attRecord);
                AttRealRecord upNotHitRecord = new AttRealRecord(attRecord.getRecordId(),operId, compId, 2, yesterday);
                attRealRecordDao.insert(upNotHitRecord);
            }
        }
        log.info("NotHitTask execute end ,date = " + yesterdayStr);
    }
}
